package com.cmb.homework0722;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author llk
 * @Description //测试类
 * @Date 21:32 2022/7/23
 **/
public class StoreThreadSample {

    public static void main(String[] args) {
        // 初始的总商品数量
        int initializeGoodsCount = StoreThread.getMapValueSum();
        //创建多个用户对象
        Customer llk = new Customer(1, "llk", new BigDecimal("20000"), new HashMap<String, Integer>());
        Customer xw = new Customer(2, "xw", new BigDecimal("1000"), new HashMap<String, Integer>());
        Customer xl = new Customer(3, "xl", new BigDecimal("2000"), new HashMap<String, Integer>());
        Customer xq = new Customer(4, "xq", new BigDecimal("3000"), new HashMap<String, Integer>());
        Customer xs = new Customer(5, "xs", new BigDecimal("4000"), new HashMap<String, Integer>());
        // 创建多个线程
        Thread threadllk = new Thread(new StoreThread(llk));
        Thread threadxw = new Thread(new StoreThread(xw));
        Thread threadxl = new Thread(new StoreThread(xl));
        Thread threadxq = new Thread(new StoreThread(xq));
        Thread threadxs = new Thread(new StoreThread(xs));
        //线程启动
        threadllk.start();
        threadxl.start();
        threadxq.start();
        threadxs.start();
        threadxw.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("购买完成");
        System.out.println("++++++++++++++");
        Integer llkBuyGoodsSum = llk.getMapValueSum();
        Integer xlBuyGoodsSum = xl.getMapValueSum();
        Integer xsBuyGoodsSum = xs.getMapValueSum();
        Integer xwBuyGoodsSum = xw.getMapValueSum();
        Integer xqBuyGoodsSum = xq.getMapValueSum();
        int userBuyGoodsSum = llkBuyGoodsSum + xlBuyGoodsSum + xsBuyGoodsSum + xqBuyGoodsSum + xwBuyGoodsSum;
        System.out.println("用户购买商品数量：" + userBuyGoodsSum);
        System.out.println("商品买出去的数量：" + (initializeGoodsCount - StoreThread.getMapValueSum()));
        System.out.println("++++++++++++++");
        for (Goods ele : StoreThread.getGoodsMap().values()
        ) {
            System.out.println(ele.getGoodsName() + "剩下：" + ele.getCounts() + "件");
        }
        System.out.println("++++++++++++++");
        System.out.println("用户" + llk.getName() + "购买了" + llk.getMapValueSum() + "件商品，剩余" +
                llk.getCredit() + "积分");
        System.out.println("用户" + xq.getName() + "购买了" + xq.getMapValueSum() + "件商品，剩余" +
                xq.getCredit() + "积分");
        System.out.println("用户" + xs.getName() + "购买了" + xs.getMapValueSum() + "件商品，剩余" +
                xs.getCredit() + "积分");
        System.out.println("用户" + xl.getName() + "购买了" + xl.getMapValueSum() + "件商品，剩余" +
                xl.getCredit() + "积分");
        System.out.println("用户" + xw.getName() + "购买了" + xw.getMapValueSum() + "件商品，剩余" +
                xw.getCredit() + "积分");
        System.out.println("用户购买商品的行为次数:" + StoreThread.getPurchaseFrequency());
    }
}
